//
//  YahooStockBrandUpdater.m
//  MarketDataColloctor
//
//  Created by jiro on 10/04/03.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "YahooStockBrandUpdater.h"
#import "../../../Commons/Commons.h"
#import "../../../DbTypes/DbTypes.h"
#import "YahooStockExchangeHtmlCollector.h"
#import "../WebResourceCollector.h"

@implementation YahooStockBrandUpdater

- (void) update
{
	//	未登録の銘柄を取得
	StockBrandJaList* brands = [ MarketJaDbOperator allBrandCodes: NO ];
	NSEnumerator* e = [ brands objectEnumerator ];
	StockBrandJa* brand;
	while ( brand = [ e nextObject ] )
	{
//		NSLog( @"銘柄更新 %@ ", brand.code );

		//	データを取得してみる
		YahooStockExchangeHtmlCollector* collector = [ [ YahooStockExchangeHtmlCollector alloc ] init ];
		NSURL* url = [ collector buildWebPageAddressFromYear: 1980 month:1 day:1 
								 toYear:3000 month:1 day:1 
								 andCode0:brand.code code1:brand.marketInitial ];
//		NSURL* url = [ collector buildWebPageAddressFromYear: 1980 
//			month: 1 day: 1 toYear: 3000 month: 1 day: 1 
//			andCode: [ NSString stringWithFormat: @"%@.%@", brand.code, brand.marketInitial ]
//		];
			
		NSArray* urls = [ NSArray arrayWithObject: url ];
		NSMutableArray* strings = [ NSMutableArray array ];

		WebResourceCollector* webCollector = [ [ WebResourceCollector alloc ] initWithYahooEnigmaOption: YES htmlEncoding: NSJapaneseEUCStringEncoding ];
		[ webCollector collectContentsOf: urls intoStrings: strings ];
		
		NSString* s = [ strings objectAtIndex: 0 ];
		NSRange range = [ s rangeOfString: @"の価格データはありません" ];

//[ s writeToFile: @"/a.html" atomically: YES ];
		//	---- 前提チェック ---- //
		if ( range.location != NSNotFound )
		{
			continue;
		}
		//	------------------- //

		NSLog( @"銘柄 %@ を追加", brand.code );
		DbOperator* dbOperator = [ DbOperator defaultOperator ];
		PGresult* res = nil;

		//	---- 銘柄存在フラグを更新 ---- //
		NSString* sql = [ NSString stringWithFormat:
			@"update %@ set %@ = true where %@ = '%@';",
			kText_StockBrandJaTable,
			kText_ExistFlag,
			kText_Code, brand.code ];
		if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] )
		{
			[ errorMessage appendFormat: @"銘柄の更新に失敗 %@\n%@\n%@\n",
				brand.code,
				[ dbOperator errorMessage ],
				sql
			];
		}
		PQclear( res );
		//	-------------------------- //

		//	---- データ収集ログを追加 ---- //
		DataCollectLog* log = [ [ DataCollectLog alloc ] init ];
		log.code0 = brand.code;
		log.code1 = brand.marketInitial;
		log.latestDate = [ NSDate dateWithNaturalLanguageString: @"1980-1-1" ];
		log.marketDataType = MarketData::Type_StockExchangeJa;
		sql = [ log sqlInsertStatement ];
		if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] )
		{
			[ errorMessage appendFormat: @"銘柄のデータ取得ログの挿入に失敗 %@\n%@\n%@\n",
				brand.code,
				[ dbOperator errorMessage ],
				sql
			];
		}
		PQclear( res );

		log.code0 = brand.code;
		log.code1 = brand.marketInitial;
		log.latestDate = [ NSDate dateWithNaturalLanguageString: @"1980-1-1" ];
		log.marketDataType = MarketData::Type_StockConfidenceRemainJa;
		sql = [ log sqlInsertStatement ];
		if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] )
		{
			[ errorMessage appendFormat: @"銘柄のデータ取得ログの挿入に失敗 %@\n%@\n%@\n",
				brand.code,
				[ dbOperator errorMessage ],
				sql
			];
		}
		PQclear( res );
		//	-------------------------- //

		[ dbOperator disconnect ];
	}
}

- (void) updateBrandName;
{
	//	---- 存在する銘柄コードを取得
	StockBrandJaList* brands = [ MarketJaDbOperator allBrandCodes: YES ];
	// ----
	
	//	---- 該当htmlを取得する
	NSEnumerator* e = [ brands objectEnumerator ];
	StockBrandJa* brand;
	DbOperator* dbOperator = [ DbOperator defaultOperator ];
	while ( YES ) {
		NSMutableArray* urls = [ NSMutableArray array ];
		NSMutableArray* htmls = [ NSMutableArray array ];
		for ( int i = 0; i < 20; i++ ) {
			brand = [ e nextObject ];
			if ( brand == nil ) {
				break;
			}
			NSURL* url = [ NSURL URLWithString: [ NSString stringWithFormat:
				@"http://stocks.finance.yahoo.co.jp/stocks/detail/?code=%@",
				brand.code ] ];
			[ urls addObject: url ];
		}
		id collector = [ [ WebResourceCollector alloc ] initWithYahooEnigmaOption: NO htmlEncoding: NSUTF8StringEncoding ];
		[ collector collectContentsOf: urls intoStrings: htmls ];

		//	---- htmlを解析しコードと企業名を取得
		NSEnumerator* htmlEnum = [ htmls objectEnumerator ];
		NSString* html;
		while ( html = [ htmlEnum nextObject ] ) {
	//		NSLog( @"%@", html );
			NSError* error = nil;
			NSXMLDocument* document = [ [ NSXMLDocument alloc ] initWithXMLString: html options: NSXMLDocumentTidyHTML error: & error ];

			NSXMLElement* element = [ document rootElement ];
			NSXMLNode* node;
			
			node = [ [ element childAtIndex: 0 ] childAtIndex: 6 ];
			id text = [ node stringValue ];
			NSRange codeStartRange = [ text rangeOfString: @"【" ];
			if ( codeStartRange.location == NSNotFound ) {
				continue;
			}
			NSRange nameRange;
			nameRange.location = 0;
			nameRange.length = codeStartRange.location;
			id name = [ text substringWithRange: nameRange ];

			NSRange codeEndRange = [ text rangeOfString: @"】" ];
			NSRange codeRange;
			codeRange.location = codeStartRange.location + 1;
			codeRange.length = codeEndRange.location - codeRange.location;
			id code = [ text substringWithRange: codeRange ];

			NSLog( @"コード:%@, 名称:%@", code, name );
			NSLog( @"%d", [ node childCount ] );
			for ( int i = 0; i < [ node childCount ]; i++ ) {
				NSLog( @"%@", [ node childAtIndex: i ]  );
			}
			
			//	---- 企業名を更新
			NSString* sql = [ NSString stringWithFormat: 
				@"update %@ set %@ = '%@' where %@ = '%@' and %@ != '';",
				kText_StockBrandJaTable, kText_Name, name, kText_Code, code, kText_Name ];
			PGresult* res = nil;
			if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] ) {
				NSLog( @"企業名の更新に失敗: %@", sql );
				NSLog( @"code: %@, name: %@", code, name );
			}
			//	----
		}
		//	----

		if ( brand == nil ) {
			break;
		}
	}
	//	----
		
	//	---- 後始末
	[ dbOperator disconnect ];
}

@end
